package com.hmdp.utils;

import cn.hutool.core.date.DateUtil;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;

@Component
public class OverallIdCreate {
    private static final long BEGIN_TIME_STAMP=1002931200L;
    private static final long MOVE_BIT=32L;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    public long generateOverAllId(String keyPrefix){
           //准备时间戳
           long timeStamp =System.currentTimeMillis()/1000 - BEGIN_TIME_STAMP;
           System.out.println(timeStamp);
           //准备序列号
            String today = DateUtil.format(new Date(), "YYYY:MM:DD");
            Long incrementNum = stringRedisTemplate.opsForValue().increment("uid" + keyPrefix + today);
            System.out.println("increment:"+incrementNum);
           //运算得到唯一全局id并返回
            return(timeStamp << MOVE_BIT) + incrementNum;
    }
}
